package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.queryObject.StrategyCatalogQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("strategyCatalog")
public class StrategyCatalogController {

    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IDestinationService destinationService;


    /**
     * 分页映射方法
     * @param model
     * @param qo    分页条件
     * @return
     */
    @RequestMapping("/list")
    public Object list(Model model, @ModelAttribute("qo")StrategyCatalogQueryObject qo){
        // Page : spring-data 提供用于 mongodb 分页的类
        Page<StrategyCatalog> page = strategyCatalogService.query(qo);
        model.addAttribute("page",page);
        return "strategyCatalog/list";
    }


    /**
     * 保存或更新
     * @param strategyCatalog    要保存的数据
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyCatalog strategyCatalog){
        strategyCatalog.setDestName(destinationService.get(strategyCatalog.getDestId()).getName());
        if (strategyCatalog.getId()!=null){
            // 如果id不为空,那么作更新操作
            strategyCatalogService.update(strategyCatalog);
        }else {
            // 保存
            strategyCatalogService.save(strategyCatalog);
        }
        return JsonResult.success();
    }


    /**
     * 删除方法
     * @param id    要删除区域的id
     * @return  JsonResult
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyCatalogService.delete(id);
        return JsonResult.success();
    }


}
